package main

import (
	"encoding/json"
	"fmt"
)

type Mon struct {
	Name string `json:"name"`
	Age  int
	Bir  string
	Sal  float64
}

func main() {

	mon := Mon{
		Name: "as",
		Age:  12,
		Bir:  "2021-11-13",
		Sal:  12.12,
	}
	// 结构体序列化
	data, err := json.Marshal(&mon)
	if err != nil {
		fmt.Println("序列化失败：", err)
	}
	fmt.Println(string(data)) // {"name":"as","Age":12,"Bir":"2021-11-13","Sal":12.12}
	testMap()
	testSlice()
}

func testMap() {
	map1 := make(map[string]interface{})
	map1["name"] = "zw"
	map1["age"] = 12
	data, err := json.Marshal(&map1)
	if err != nil {
		fmt.Println("序列化失败：", err)
	}
	fmt.Println(string(data)) //{"age":12,"name":"zw"}
}

func testSlice() {
	var slice1 []map[string]interface{}
	map1 := make(map[string]interface{})
	map1["name"] = "zw"
	map1["age"] = 12
	slice1 = append(slice1, map1)
	data, err := json.Marshal(&slice1)
	if err != nil {
		fmt.Println("序列化失败：", err)
	}
	fmt.Println(string(data)) //[{"age":12,"name":"zw"}]
}
